package com.google.code.cubeirc.editor;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;

public class HistoryAdapter extends KeyAdapter {

	private String[] history = new String[255];

	private StyledText source;
	private int count = 0;
	private int index = 0;
	@Override
	public void keyPressed(KeyEvent e) {

		super.keyPressed(e);

		source = (StyledText)e.getSource();

		if (e.keyCode == SWT.ARROW_UP)
		{		
			if (index-1 >= 0)
				index--;

			source.setText(history[index]);
			source.setSelection(0, source.getText().length());

		}
		if (e.keyCode == SWT.ARROW_DOWN)
		{			
			if (index+1 < count)
				index++;
			
			source.setText(history[index]);
			source.setSelection(0, source.getText().length());

		}


		if (e.keyCode == 13)
		{
			history[count] = source.getText().replace("\r\n", "");
			count++;
		}
	}

}
